"""
Copyright (c) 2023 - present SKbioinfo
"""

from flask_restful import Resource
from flask import jsonify,request
from flask_login import login_required

from apps.API.project.db_project import *
from apps.API.utils.log_helper import lg
from apps.API.connector.db_common import *
from apps.API.common.db_table_config import *

'''
    Project infos.NGS_API 
'''
class interfaceProjectID(Resource):
     def delete(self,id):
        try:
            ### SQL: delete project in database ###
            delete_sql = "DELETE FROM {table_name} WHERE {db_name} = \"{col_value}\"".format(
                table_name=common_tables.project_db["name"],
                col_value=id,
                db_name="id"
            )
            delete_info(delete_sql = delete_sql)
        except Exception as e:
            lg.error(e)

class interfaceProject(Resource):
    # @login_required
    def get(self):
        try:
            projects = []
            ### receive projects infos from database ###
            project_base_info=get_project_info()
            for info in project_base_info:
                projects.append({
                    "id":info["id"],
                    "project_id":info["project_id"],
                    "project_name":info["project_name"]
                })
            ### collect project infos ###
            response=jsonify(projects)
            response.status_code = 202
            return response
        except Exception as e:
            lg.error(e)
    
    def post(self):
        try:
            select_proj_ids = request.json["project_id"]
            query_proj_list = ""
            for project_id in select_proj_ids:
                query_proj_list += "\"" + project_id + "\","
            query_proj_list=query_proj_list.rstrip(",")
            ### SQL: query selected projects ###
            cols = '''
                id,
                project_id,
                client_id,
                sample_id,
                sample_name,
                lot_number,
                size,
                sample_amount,
                current_amount,
                disp_amount,
                disp_size,
                status,
                arrive_time,
                instock_time,
                create_time,
                modify_time
            '''
            query_proj_sql='''
                SELECT {cols} FROM {table_name} WHERE {select_col} IN ({select_values})
            '''.format(
                cols=cols,
                table_name=common_tables.sample_db["name"],
                select_col="project_id",
                select_values=query_proj_list
            )
            base_infos=dic_query_info(query_proj_sql)
            project_infos=[]
            for info in base_infos:
                info["instock_time"]=shift_format(info["instock_time"])
                # info["arrive_time"]=shift_format(info["arrive_time"]) 
                project_infos.append(info)
            response=jsonify(project_infos)
            response.status_code = 202
            return response
        except Exception as e:
            lg.error(e)